﻿using System.Text;
using System.Collections.ObjectModel;
using MarginTrading.Entity;
using MarginTrading.Common;
using System.Data.OracleClient;
using System.Data;

namespace MarginTrading.Services
{
    public class UserAccountServices
    {
        private static UserAccount GetUserAccountFromDataReader(IDataReader dataReader)
        {
            UserAccount retVal = null;
            if (dataReader != null && !dataReader.IsClosed)
            {
                retVal = new UserAccount();
                retVal.UserName = dataReader["USERNAME"].ToString();
                retVal.Password = dataReader["PASSWORD"].ToString();
                int userType = 0;
                if (int.TryParse(dataReader["USER_TYPE"].ToString(), out userType))
                    retVal.UserType = userType;
            }
            return retVal;
        }

        public static UserAccount GetUserAccountByUserName(string userName)
        {
            UserAccount retVal = null;
            OracleConnection con = new OracleConnection();

            try
            {
                
                con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                con.Open();
                OracleCommand cmd = new OracleCommand();
                cmd.CommandText = "select * from user_account where username = '" + userName + "'";
                cmd.Connection = con;

                using (IDataReader dataReader = cmd.ExecuteReader())
                {
                    if (dataReader.Read())
                    {
                        retVal = GetUserAccountFromDataReader(dataReader);
                    }
                }
            }
            catch (System.Exception exc)
            {
                string s = exc.Message;
                throw;
            }
            finally
            {
                con.Close();
            }
            return retVal;
        }

        public static UserAccountCollection GetUserAccountCollection()
        {
            UserAccountCollection retVal = null;
            OracleConnection con = new OracleConnection();

            try
            {
                con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                con.Open();
                OracleCommand cmd = new OracleCommand();
                cmd.CommandText = "select * from user_account";
                cmd.Connection = con;

                using (IDataReader dataReader = cmd.ExecuteReader())
                {
                    retVal = new UserAccountCollection();
                    while (dataReader.Read())
                    {
                        UserAccount item = GetUserAccountFromDataReader(dataReader);
                        if (item != null)
                            retVal.Add(item);
                    }
                }
            }
            catch (System.Exception exc)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
            return retVal;
        }

        public static DataTable GetUserAccountCollectionDataTable()
        {
            DataTable retVal = null;
            OracleConnection con = new OracleConnection();

            try
            {
                con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                con.Open();
                OracleCommand cmd = new OracleCommand();
                cmd.CommandText = "select * from user_account";
                cmd.Connection = con;

                using (IDataReader dataReader = cmd.ExecuteReader())
                {
                    if (dataReader != null)
                    {
                        retVal = new DataTable();
                        retVal.Load(dataReader);
                    }
                }
            }
            catch (System.Exception exc)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
            return retVal;
        }


        public static DataTable GetCustomUserAccountCollectionDataTable()
        {
            DataTable retVal = null;
            OracleConnection con = new OracleConnection();

            try
            {
                con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                con.Open();
                OracleCommand cmd = new OracleCommand();
                cmd.CommandText = "SELECT USERNAME, CASE WHEN USER_TYPE = '1' THEN  'Administrator' ELSE 'Normal User' END AS USER_TYPE FROM USER_ACCOUNT";
                cmd.Connection = con;

                using (IDataReader dataReader = cmd.ExecuteReader())
                {
                    if (dataReader != null)
                    {
                        retVal = new DataTable();
                        retVal.Load(dataReader);
                    }
                }
            }
            catch (System.Exception exc)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
            return retVal;
        }


        public static void Add(UserAccount userAccount)
        {
            OracleConnection con = new OracleConnection();

            try
            {
                con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                con.Open();
                OracleCommand cmd = new OracleCommand();
                cmd.CommandText = "insert into user_account(USERNAME,PASSWORD,USER_TYPE)" +
                                  "values(:v1,:v2,:v3)";
                cmd.Parameters.Add(":v1", OracleType.VarChar).Value = userAccount.UserName;
                cmd.Parameters.Add(":v2", OracleType.VarChar).Value = userAccount.Password;
                cmd.Parameters.Add(":v3", OracleType.Number).Value = userAccount.UserType;

                cmd.Connection = con;
                cmd.ExecuteNonQuery();

            }
            catch (System.Exception exc)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
        }

        public static void Delete(string userName)
        {
            OracleConnection con = new OracleConnection();
            try
            {
                con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                con.Open();
                OracleCommand cmd = new OracleCommand();
                cmd.CommandText = "delete from user_account where username = '" + userName + "'";
                cmd.Connection = con;

                cmd.ExecuteNonQuery();
            }
            catch (System.Exception exc)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
        }

        public static void Update(UserAccount userAccount)
        {
            OracleConnection con = new OracleConnection();

            try
            {
                con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                con.Open();
                OracleCommand cmd = new OracleCommand();
                cmd.CommandText = "update user_account set " +
                                  "PASSWORD = :v2, " +
                                  "USER_TYPE = :v3 " +
                                  "where USERNAME = :v1";
                cmd.Parameters.Add(":v1", OracleType.VarChar).Value = userAccount.UserName;
                cmd.Parameters.Add(":v2", OracleType.VarChar).Value = userAccount.Password;
                cmd.Parameters.Add(":v3", OracleType.Number).Value = userAccount.UserType;

                cmd.Connection = con;
                cmd.ExecuteNonQuery();

            }
            catch (System.Exception exc)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
        }
    }
}
